import Shop from "../../config/shop";
export function formatDate(date, formatStr) {
  var arrWeek = ["日", "一", "二", "三", "四", "五", "六"],
    str = formatStr
      .replace(/yyyy|YYYY/, date.getFullYear())
      .replace(/yy|YY/, addZero(date.getFullYear() % 100, 2))
      .replace(/mm|MM/, addZero(date.getMonth() + 1, 2))
      .replace(/m|M/g, date.getMonth() + 1)
      .replace(/dd|DD/, addZero(date.getDate(), 2))
      .replace(/d|D/g, date.getDate())
      .replace(/hh|HH/, addZero(date.getHours(), 2))
      .replace(/h|H/g, date.getHours())
      .replace(/ii|II/, addZero(date.getMinutes(), 2))
      .replace(/i|I/g, date.getMinutes())
      .replace(/ss|SS/, addZero(date.getSeconds(), 2))
      .replace(/s|S/g, date.getSeconds())
      .replace(/w/g, date.getDay())
      .replace(/W/g, arrWeek[date.getDay()]);
  return str;
}
function addZero(v, size) {
  for (var i = 0, len = size - (v + "").length; i < len; i++) {
    v = "0" + v;
  }
  return v + "";
}
// export function formatTime (date) {
//   const year = date.getFullYear()
//   const month = date.getMonth() + 1
//   const day = date.getDate()
//
//   const hour = date.getHours()
//   const minute = date.getMinutes()
//   const second = date.getSeconds()
//
//   const t1 = [year, month, day].map(formatNumber).join('/')
//   const t2 = [hour, minute, second].map(formatNumber).join(':')
//
//   return `${t1} ${t2}`
// }

export function dictMatching(list, val) {
  if (val == null) {
    return;
  }
  let newList = list.filter((item) => item.dictValue == val);
  return newList[0].dictLabel;
}

export function filterParams(obj) {
  let params = {};
  Object.entries(obj).map((item, index) => {
    if (item[1] != "") {
      params[item[0]] = item[1];
    }
  });
  return params;
}

export function previewImage(event) {
  let urls = event.currentTarget.dataset.urls;
  let id = event.currentTarget.id;
  let imgs = [];
  urls.map((item) => {
    imgs.push(item.startsWith("http") ? item : Shop.IMG_URL + item);
  });
  wx.previewImage({
    current: id.startsWith("http") ? id : Shop.IMG_URL + id, // 当前显示图片的http链接
    urls: imgs, // 需要预览的图片http链接列表
  });
}

export function unique(arr) {
  const res = new Map();
  return arr.filter((arr) => !res.has(arr.id) && res.set(arr.id, 1));
}

export default {
  // formatNumber,
  // formatTime,
  dictMatching,
  formatDate,
  filterParams,
  previewImage,
  unique,
};
